package itstudio.instructor.fragment;

import itstudio.app.wq.R;
import itstudio.instructor.config.Config;
import itstudio.instructor.config.MyApplication;
import itstudio.instructor.http.TwitterRestClient;
import itstudio.instructor.service.DownloadService;
import itstudio.instructor.service.DownloadService.DownloadBinder;
import itstudio.instructor.ui.LoginActivity;
import itstudio.instructor.ui.MainActivity;
import itstudio.instructor.ui.FeedBackActivity;
import itstudio.instructor.ui.AboutActivity;
import itstudio.instructor.ui.AboutActivity.ICallbackResult;
import itstudio.instructor.util.FileUtil;
import itstudio.instructor.util.LoginUtil;
import itstudio.instructor.util.SharedPreferencesUtil;
import itstudio.instructor.widget.CustomDialog;
import itstudio.instructor.widget.UISwitchButton;

import java.io.File;
import java.text.DecimalFormat;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;

/**
 * @Description 设置 Fragment
 * 
 * @author MR.Wang
 * 
 * @date 2014-7-5 上午1:13:26
 * 
 * @version V1.0
 */

@SuppressLint("ValidFragment")
public class FragmentSetting extends Fragment {

	//
	private CustomDialog.Builder ibuilder;
	Dialog dialog = null;
	private View rootView;
	private Context context;
	private View aboutView;
	private View feedbackView;
	private View cleanView;
	private Button exit_btn;
	private TextView cache_size;
	private String size;
	private UISwitchButton sb_auto_update;
	private UISwitchButton sb_auto_login;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		context = inflater.getContext();

		if (rootView == null) {
			rootView = inflater.inflate(R.layout.fragment_setting, container,
					false);
			Handler handler = new Handler();
			handler.postDelayed(run, 400);
			findById();
			setOnclickListener();

		}
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		}
		
		return rootView;
	}

	Runnable run = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub

		}
	};

	public void onResume() {
		super.onResume();
		if (cache_size != null) {
			
			DecimalFormat df = new java.text.DecimalFormat("#0.0");
			File file = new File(Config.PACKAGE_PATH);
			size = df.format(FileUtil.getDirSize(file));
			cache_size.setText(size + "m");
		}

	};

	private void findById() {
		aboutView = rootView.findViewById(R.id.about);
		feedbackView = rootView.findViewById(R.id.feedbackview);
		cleanView = rootView.findViewById(R.id.cleanview);
		exit_btn = (Button) rootView.findViewById(R.id.exit);
		cache_size = (TextView) rootView.findViewById(R.id.cache_size);
		sb_auto_update = (UISwitchButton) rootView.findViewById(R.id.sb_autoupdate);
		sb_auto_login = (UISwitchButton) rootView.findViewById(R.id.sb_autologin);
		DecimalFormat df = new java.text.DecimalFormat("#0.0");
		File file = new File(Config.PACKAGE_PATH);
		size = df.format(FileUtil.getDirSize(file));
		cache_size.setText(size + "m");
		String update = SharedPreferencesUtil.get(getActivity(), "auto_update");
		String login = SharedPreferencesUtil.get(getActivity(), "auto_login");
		if("1".equals(update)){
			sb_auto_update.setChecked(false);
		}
		if("1".equals(login)){
			sb_auto_login.setChecked(false);
		}
	}

	private void setOnclickListener() {
		MyOnClickListener cllick = new MyOnClickListener();
		feedbackView.setOnClickListener(cllick);
		aboutView.setOnClickListener(cllick);
		cleanView.setOnClickListener(cllick);
		exit_btn.setOnClickListener(cllick);
		sb_auto_update.setOnCheckedChangeListener(new UpdateOnCheckedChangeListener() {});
		sb_auto_login.setOnCheckedChangeListener(new LoginOnCheckedChangeListener() {});
		if (MyApplication.user != null) {
			exit_btn.setVisibility(View.VISIBLE);
		}

	}

	private class UpdateOnCheckedChangeListener implements
			CompoundButton.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			if (sb_auto_update.isChecked()) {
				SharedPreferencesUtil.add(context, "auto_update", "0");
			} else {
				SharedPreferencesUtil.add(context, "auto_update", "1");
			}

		}
	}
	private class LoginOnCheckedChangeListener implements
	CompoundButton.OnCheckedChangeListener {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			if (sb_auto_login.isChecked()) {
				SharedPreferencesUtil.add(context, "auto_login", "0");
			} else {
				SharedPreferencesUtil.add(context, "auto_login", "1");
			}
			
		}
	}

	private class MyOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			switch (v.getId()) {
			case R.id.about:
				intent.setClass(getActivity(), AboutActivity.class);
				startActivity(intent);
				break;
			case R.id.feedbackview:
				if(MyApplication.user!=null){
					intent.setClass(getActivity(), FeedBackActivity.class);
					startActivity(intent);
				}
				else{
					
					intent.setClass(getActivity(), LoginActivity.class);
					startActivity(intent);
				}
				break;
			case R.id.cleanview:

				showCleanDialog();
				break;
			case R.id.exit:
				showExitDialog();

				break;

			default:
				break;
			}

		}
	}

	private void showCleanDialog() {

		ibuilder = new CustomDialog.Builder(getActivity());
		ibuilder.setTitle(R.string.cleanCache);
		String res = getResources().getString(R.string.cleanCache_message);
		String.format(res, size);
		ibuilder.setMessage(String.format(res, size));
		ibuilder.setPositiveButton(R.string.confirm,
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						File file = new File(Config.PACKAGE_PATH);
						ImageLoader.getInstance().clearDiscCache();
						FileUtil.deleteFile(new File(Config.TEMP_PATH));
						dialog.dismiss();
						cache_size.setText("0M");
						size = "0";
					}
				});
		ibuilder.setNegativeButton(R.string.cancel, null);
		dialog = ibuilder.create();
		dialog.show();

	}

	private void showExitDialog() {

		ibuilder = new CustomDialog.Builder(getActivity());
		ibuilder.setTitle(R.string.exit);
		ibuilder.setMessage(R.string.exit_question);
		ibuilder.setPositiveButton(R.string.confirm,
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						MyApplication.user = null;
						LoginUtil.LogOut(getActivity());
						LeftDrawerFragment.setInfo();
						dialog.dismiss();
						exit_btn.setVisibility(View.GONE);
					}
				});
		ibuilder.setNegativeButton(R.string.cancel, null);
		dialog = ibuilder.create();
		dialog.show();

	}
}
